//
//  SendedJobListViewController.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/6/14.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "SendedJobListViewController.h"
#import "SendedJobListTableViewCell.h"
#import "IssueJobViewController.h"
#import "UpdateIssueJobViewController.h"

@interface SendedJobListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *mainTableView; // 主框架
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) NSMutableArray *muDataArray;
@property (nonatomic, strong) NSMutableArray *datamuArray;
@property (nonatomic, strong) NSString *jobId;
@end

@implementation SendedJobListViewController
- (void)getJobsList {
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=joblist" parameters:@{@"uid":[UserModel sharedInstanced].userId,@"token":[UserModel sharedInstanced].token} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"error_code"] isEqualToString:@"0"]) {
            self.datamuArray = responseObject[@"data"];
        }else{
            ShowMessage(responseObject[@"message"]);
        }
        [self.mainTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
    }];
}

- (void)deleteJobsList {
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=deletesjob" parameters:@{@"uid":[UserModel sharedInstanced].userId,@"token":[UserModel sharedInstanced].token,@"jobid":self.jobId} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"error_code"] isEqualToString:@"0"]) {
            ShowMessage(@"删除成功");
            [self getJobsList];
        }else{
            ShowMessage(@"删除失败");
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.title = @"发布岗位";
    [self.view addSubview:self.mainTableView];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getJobsList];
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datamuArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SendedJobListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SendedJobListTableViewCell class])];
    cell.JobsDataDic = self.datamuArray[indexPath.row];
    cell.editBtnBlock = ^{
        NSLog(@"编辑回调成功啊");
        UpdateIssueJobViewController *VC = [[UpdateIssueJobViewController alloc]init];
        VC.dataDic = self.datamuArray[indexPath.row];
        [self.navigationController pushViewController:VC animated:NO];
    };
    cell.deleteBtnBlock = ^{
        NSLog(@"删除回调成功");
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"信息提示" message:@"您确定要删除该岗位吗" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"确认");
            self.jobId = self.datamuArray[indexPath.row][@"jobid"];
            [self deleteJobsList];
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击了取消");
        }];
        
        [alert addAction:action1];
        [alert addAction:action2];
        [self presentViewController:alert animated:YES completion:nil];
    };
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60 / HEIGHT_6S_SCALE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100 / HEIGHT_6S_SCALE;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc]init];
    footerView.backgroundColor = [UIColor clearColor];
    UIButton * sendServerBtn = [[UIButton alloc]init];
    sendServerBtn.backgroundColor = getColor(mainColor);
    [sendServerBtn setTitleColor:getColor(whiteColor) forState:UIControlStateNormal];
    [sendServerBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [sendServerBtn setTitle:@"+ 发布岗位" forState:UIControlStateNormal];
    sendServerBtn.layer.cornerRadius = 5;
    sendServerBtn.layer.masksToBounds = YES;
    [footerView addSubview:sendServerBtn];
    
    [sendServerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(footerView.mas_centerY);
        make.left.equalTo(footerView.mas_left).offset(20 / WIDTH_6S_SCALE);
        make.right.equalTo(footerView.mas_right).offset(-20 / WIDTH_6S_SCALE);
        make.height.mas_equalTo(50 / HEIGHT_6S_SCALE);
    }];
    return footerView;
}

- (void) btnAction:(UIButton *)btn
{
    IssueJobViewController *VC = [[IssueJobViewController alloc]init];
    [self.navigationController pushViewController:VC animated:NO];
    NSLog(@"发布成功");
}
#pragma mark - init
- (UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaBottomHeight - SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _mainTableView.backgroundColor = getColor(@"ECECEC");
        _mainTableView.tableHeaderView = self.headerView;
        _mainTableView.tableFooterView = self.footerView;
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.showsHorizontalScrollIndicator = NO;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        // 动态行高
        _mainTableView.rowHeight = UITableViewAutomaticDimension;
        // 预估行高
        _mainTableView.estimatedRowHeight = 80;
        [_mainTableView registerClass:[SendedJobListTableViewCell class] forCellReuseIdentifier:NSStringFromClass([SendedJobListTableViewCell class])];
    }
    return _mainTableView;
}
- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
        _headerView.backgroundColor = [UIColor grayColor];
    }
    return _headerView;
}
- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}
- (NSMutableArray *)datamuArray
{
    if (!_datamuArray) {
        _datamuArray = @[].mutableCopy;
    }
    return _datamuArray;
}
@end
